var ClassifyMaterialDataModel = function (materialList, option) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (materialList, option) {
      var materials = [];
      var materialObj = {};
      var classifyOption = undefined;
      if (option !== undefined) {
        var wasteRateList = option.wasteRateList;
        var orderTotal = option.orderTotal;
        var orderCountList = option.orderCountList;
        var wasteRateValue = 0;
        for (var i = 0; i < orderCountList.length; i++) {
          var count = orderCountList[i];
          var wasteValue = ProduceMaterialDataModel.getWasteRateValue(count, wasteRateList);
          wasteRateValue += wasteValue;
        }
        classifyOption = {
          orderTotal: orderTotal,
          wasteRateValue: wasteRateValue
        };
      }

      for (var i = 0; i < materialList.length; i++) {
        var material = materialList[i];
        material = new ProduceMaterialDataModel(material, 'material', classifyOption);
        materials.push(material);

        levelRankOne = material.levelRankOne;
        if (levelRankOne === '') {
          levelRankOne = 'noClassify';
        }
        var arr = materialObj[levelRankOne];
        if (arr === undefined) {
          arr = [];
          materialObj[levelRankOne] = arr;
        }
        arr.push(material);
      }
      self.materialList = materials;
      self.materialObj = materialObj;
    }
  };

  this.hasClassify = function (classify, isGroup) {
    if (!isGroup) {
      return true;
    }

    var materialObj = this.materialObj;
    var obj = materialObj[classify.classifyName];
    return obj !== undefined;
  };

  this.hasData = function () {
    var materialList = this.materialList;
    return materialList.length > 0;
  };

  this.getMaterialList = function (fieldObj, isGroup) {
    if (!isGroup) {
      return self.materialList;
    }
    var materialObj = this.materialObj;
    var id = fieldObj.id;
    var materialList = materialObj[id];
    return materialList;
  };

  this.getObject = function (option) {
    var materialList = this.materialList;
    var materials = [];
    for (var i = 0; i < materialList.length; i++) {
      var material = materialList[i];
      material = material.getObject(option);
      material.lineNo = '-1';
      materials.push(material);
    }
    return materials;
  };

  this.setCheckUpdate = function (oldData) {
    var oldDataGroup = _.groupBy(oldData.materialList, 'materialType');
    var materialList = this.materialList;
    var updateStatusObj = {};
    for (var i = 0; i < materialList.length; i++) {
      var material = materialList[i];
      var materialType = material.materialType;
      var oldMaterial = oldDataGroup[materialType];
      if (oldMaterial === undefined) {
        updateStatusObj[materialType] = true;
      } else {
        oldMaterial = oldMaterial[0];
        updateStatusObj[materialType] = material.getCheckUpdateObj(oldMaterial);
      }
    }
    this.updateStatusObj = updateStatusObj;
  };

  this.checkUpdate = function (columnKey, material) {
    var updateStatusObj = this.updateStatusObj;
    if (updateStatusObj === undefined) {
      return false;
    }

    var materialType = material.materialType;
    var obj = updateStatusObj[materialType];
    if (obj === true) {
      return true;
    }

    var arr = columnKey.split('_');
    var field = arr[1];
    if (['orderTotal', 'yltsTotal', 'feedTotal'].indexOf(field) === -1) {
      return false;
    }
    return obj[field];
  };

  fn.initData(materialList, option);
};
